// pages/tsignin/tsignin.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    no: -1,
    showStart: false,
    showEnd: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let no = options.no

    // 判断是否存在运行中的签到
    wx.cloud.callFunction({
      name: "isExistSignin",
      data: {
        classNumber: no
      }
    }).then(res => {
      console.log(res)
      if (res.result.running == true) {
        this.setData({
          no: options.no,
          showStart: true,
          showEnd: false,
        })
      } else {
        this.setData({
          no: options.no,
          showStart: false,
          showEnd: true,
        })
      }
    }).catch(res => {
      this.setData({
        no: options.no,
        showStart: true,
        showEnd: true,
      })
    })

  },
  onStart: function(e) {
    wx.cloud.callFunction({
      name: 'createSignin',
      data: {
        no: this.data.no
      }
    }).then(
      res => {
        this.setData({
          showStart: true,
        })
      }
    ).catch(
      res => {
        console.log(res)
      }
    )
  },
  onEnd: function(e) {
    wx.cloud.callFunction({
      name: 'finishSignin',
      data: {
        no: this.data.no
      }
    }).then(
      res => {
        this.setData({
          showEnd: true,
          showStart: false
        })
      }
    ).catch(
      res => {
        console.log(res)
      }
    )
  }
})